from selenium.webdriver.edge.webdriver import WebDriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait


class BasePage:
    driver: WebDriver

    def __init__(self, driver: WebDriver):
        self.driver = driver

    def back(self):
        self.driver.back()

    def get_title(self):
        return self.driver.title.title()

    def is_displayed(self, locator):
        return self.driver.find_element(locator).is_displayed()

    def cookie_is_set(self, name: str):
        for c in self.driver.get_cookies():
            if c["name"] == name:
                return True
        return False

    def check_element(self, locator):
        if len(self.driver.find_elements(locator[0], locator[1])) > 0:
            return True
        return False

    def wait_new_page(self, url: str, time: int = 10):
        WebDriverWait(self.driver, time).until(EC.url_contains(url))

    def find_element(self, locator, time: int = 10):
        return WebDriverWait(self.driver, time).until(EC.presence_of_element_located(locator),
                                                      message=f"Can't find element by locator {locator}")

    def find_elements(self, locator, time: int = 10):
        return WebDriverWait(self.driver, time).until(EC.presence_of_all_elements_located(locator),
                                                      message=f"Can't find elements by locator {locator}")

    def get_element_text(self, locator, time: int = 10):
        return self.find_element(locator, time).text

    def go_to_site(self, url: str):
        self.driver.get(url)

        return self
